package com.yunhuajinfu.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzy.okhttputils.model.HttpParams;
import com.yunhuajinfu.R;
import com.yunhuajinfu.adapter.PayHongbaolistAdapter;
import com.yunhuajinfu.base.BaseActivity;
import com.yunhuajinfu.dialog.ExitLoaginDialog;
import com.yunhuajinfu.entity.BaoLiInfo;
import com.yunhuajinfu.entity.BorrowList;
import com.yunhuajinfu.entity.Huifu;
import com.yunhuajinfu.entity.Pagess;
import com.yunhuajinfu.entity.RedList;
import com.yunhuajinfu.http.HttpCallBack;
import com.yunhuajinfu.http.HttpUtils;
import com.yunhuajinfu.mian.Config;
import com.yunhuajinfu.response.BorroDetailsRes;
import com.yunhuajinfu.response.CitizenscertificationRes;
import com.yunhuajinfu.response.InvestmentStepOneRes;
import com.yunhuajinfu.response.PayHongBaoRes;
import com.yunhuajinfu.tool.SharedPreferencesUtils;
import com.yunhuajinfu.tool.StatusBarUtil;
import com.yunhuajinfu.tool.Tool;
import com.yunhuajinfu.util.DesUtil;
import com.yunhuajinfu.util.HtmlFormUtil;
import com.yunhuajinfu.view.ClearEditText;
import com.yunhuajinfu.view.ColorArcProgressBar;
import com.yunhuajinfu.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 立即投资   投资详情页
 */
public class TouZiDetailsActivity extends BaseActivity {
    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.mPieChartView)
    ColorArcProgressBar mPieChartView;
    @Bind(R.id.tv_availableTenderMoney)
    TextView tvAvailableTenderMoney;
    @Bind(R.id.tv_liji_touzi)
    TextView mtv_lijiTouzi;
    @Bind(R.id.tv_biao_type)
    TextView mtvBiaoType;
    @Bind(R.id.mViewNeedOffset)
    LinearLayout mViewNeedOffset;
    //
    private BorrowList borroInfo;
    private BaoLiInfo mBaoLiInfo;
    //
    private BaoLiInfo mZhiYaInfo;
    private String borrowType;
    private BorroDetailsRes res;      //
    private Pagess mInvestmentInfo;   //点击我的投资列表返回的数据
    //
    private Dialog payDialog;
    private Dialog mNextpayDialog;
    //
    InvestmentStepOneRes mInvestmentStepOneRes;     //投资第一步响应数据
    private PayHongBaoRes payres;           //第二步红包响应数据
    private String redUserEid;
    //
    private PayHongbaolistAdapter mhongbaolistAdapter;
    //
    private String type;   //  为startActivityForResult所用

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_tou_zi_details);
        StatusBarUtil.setTranslucentForImageView(this,0,mViewNeedOffset);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        type = getIntent().getStringExtra("type");
        //
        borroInfo = (BorrowList) getIntent().getSerializableExtra("borroInfo");
        mBaoLiInfo = (BaoLiInfo) getIntent().getSerializableExtra("mBaoLiInfo");
        //
        mInvestmentInfo = (Pagess) getIntent().getSerializableExtra("mInvestmentInfo");


    }

    @Override
    protected void initView() {
        if (null != borroInfo) {
            titlebar.setTitleBar(R.drawable.back_selector, borroInfo.getBorrowTitle(), 0);
        }
        if (null != mBaoLiInfo) {
            titlebar.setTitleBar(R.drawable.back_selector, mBaoLiInfo.getBorrowTitle(), 0);
        }
        if (null != mInvestmentInfo) {
            titlebar.setTitleBar(R.drawable.back_selector, mInvestmentInfo.getBorrowTitle(), 0);
        }


    }

    @Override
    protected void setData() {

        getShowBorrow();

    }


    @OnClick({R.id.titlebar_iv_back, R.id.tv_liji_touzi, R.id.lly_djck})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:

                finish();

                break;

            case R.id.lly_djck:
                Intent intent = new Intent(TouZiDetailsActivity.this, DetalInforDetailsActivity.class);
                if (null != borroInfo && null != borrowType) {
                    intent.putExtra("borrowType", borrowType);
                    intent.putExtra("borroInfo", borroInfo);
                    startActivity(intent);
                }
                if (null != mBaoLiInfo && null != borrowType) {
                    intent.putExtra("borrowType", borrowType);
                    intent.putExtra("mBaoLiInfo", mBaoLiInfo);
                    startActivity(intent);
                }
                if(null!=mInvestmentInfo && null!=borrowType){
                    intent.putExtra("borrowType", borrowType);
                    intent.putExtra("mInvestmentInfo", mInvestmentInfo);
                    startActivity(intent);

                }




                break;


            case R.id.tv_liji_touzi:    //立即投资

                //判断是否可以点击
                if (null != res) {
                    if (mtv_lijiTouzi.getText().equals("立即投资") && Tool.compare_date(res.getAllowTenderTimeStr())) {
                        mtv_lijiTouzi.setClickable(true);
                        //2判断是否登录了
                        if (SharedPreferencesUtils.getInstance().isLogin(TouZiDetailsActivity.this, "userAccount")) {
                            // 投资第一步获取数据
                            goInvestment();

                        } else {
                            Intent intent1 = new Intent(TouZiDetailsActivity.this, LoginActivity.class);
                            startActivity(intent1);
                        }

                    } else {
                        mtv_lijiTouzi.setClickable(false);
//                        Toast.makeText(TouZiDetailsActivity.this, mtv_lijiTouzi.getText().toString().trim(), Toast.LENGTH_SHORT).show();
                    }
                }


                break;

            default:
        }
    }

    /**
     * 弹出支付对话框
     */

    private void showDialg() {
        View payView = LayoutInflater.from(this).inflate(R.layout.pay_layout_view, null);
        payDialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        payDialog.setContentView(payView);
        Window dialogWindow = payDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置离底部的距离
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 20;
        dialogWindow.setAttributes(lp);
        ImageView imageViewClose = (ImageView) payView.findViewById(R.id.iv_pay_close);

        imageViewClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payDialog.dismiss();
            }
        });

        //点击下一步

        final ClearEditText minAmount = (ClearEditText) payView.findViewById(R.id.et_minAmount);
        //可用余额
        final TextView availableMoney = (TextView) payView.findViewById(R.id.tv_availableMoney);
        final TextView tv_shouyi = (TextView) payView.findViewById(R.id.tv_shouyi);
        if (null != mInvestmentStepOneRes) {
            minAmount.setHint("起投金额:" + mInvestmentStepOneRes.getMinAmount() + ",最高:" + mInvestmentStepOneRes.getMaxAmount());
            availableMoney.setText(mInvestmentStepOneRes.getAvailableMoney());
        }
        TextView InvestmentStepOne = (TextView) payView.findViewById(R.id.tv_InvestmentStepOne);

        //ecditext监听文本变化
        minAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String textstr = s.toString();
                if (!textstr.equals("")) {
//                    if((Float.parseFloat(textstr))>(Float.parseFloat(mInvestmentStepOneRes.getMinAmount()))&&(Float.parseFloat(textstr))<(Float.parseFloat(mInvestmentStepOneRes.getMaxAmount()))){
//                    tv_shouyi.setText((Float.parseFloat(minAmount.getText().toString().trim())) * (Float.parseFloat(mInvestmentStepOneRes.getAnnualInterestRate()) / 100) + "");
//                    tv_shouyi.setText(savefloat((Float.parseFloat(minAmount.getText().toString().trim())) * (Float.parseFloat(mInvestmentStepOneRes.getAnnualInterestRate()) / 100)));
//                    tv_shouyi.setText(YieldUtils.lixi(minAmount.getText().toString().trim(), mInvestmentStepOneRes));
//                    tv_shouyi.setText(Float.parseFloat(minAmount.getText().toString().trim())*Float.parseFloat(mInvestmentStepOneRes.getInterest())+"");
//                    tv_shouyi.setText(savefloat(Float.parseFloat(minAmount.getText().toString().trim()) * Float.parseFloat(mInvestmentStepOneRes.getInterest())));
//
// }else {
//                    }
                } else {
//                    tv_shouyi.setText("0.00");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

//点击下一步
        InvestmentStepOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(minAmount.getText().toString().trim())) {
                    minAmount.setShakeAnimation();
                    Toast.makeText(TouZiDetailsActivity.this, "起投金额不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if ((Float.parseFloat(minAmount.getText().toString().trim())) > (Float.parseFloat(mInvestmentStepOneRes.getAvailableMoney()))) {
                    minAmount.setShakeAnimation();
                    //判断是否实名认证
                    getAccountConfig(2);
//                    Toast.makeText(TouZiDetailsActivity.this, "可用余额" + mInvestmentStepOneRes.getAvailableMoney() + "元,请输入不大于此金额的数字", Toast.LENGTH_SHORT).show();
//                    Toast.makeText(TouZiDetailsActivity.this, "可用余额" + mInvestmentStepOneRes.getAvailableMoney() + "元,请充值", Toast.LENGTH_SHORT).show();
                    //弹出对话框


                    return;
                } else if ((Float.parseFloat(minAmount.getText().toString().trim())) > (Float.parseFloat(mInvestmentStepOneRes.getMaxAmount()))) {
                    minAmount.setShakeAnimation();
                    Toast.makeText(TouZiDetailsActivity.this, "此标剩余可投金额" + mInvestmentStepOneRes.getMaxAmount() + "元,请输入不大于此金额的数字", Toast.LENGTH_SHORT).show();
                    return;
                } else if ((Float.parseFloat(minAmount.getText().toString().trim())) < (Float.parseFloat(mInvestmentStepOneRes.getMinAmount()))) {
                    minAmount.setShakeAnimation();
                    Toast.makeText(TouZiDetailsActivity.this, "起投金额低于目标起投金额," + mInvestmentStepOneRes.getMinAmount() + "起投", Toast.LENGTH_SHORT).show();
                    return;

                }
//                else if ((Float.parseFloat(minAmount.getText().toString().trim()) > (Float.parseFloat(mInvestmentStepOneRes.getMaxAmount())))) {
//                    minAmount.setShakeAnimation();
//                    Toast.makeText(TouZiDetailsActivity.this, "您以超出最大投资金额," + mInvestmentStepOneRes.getMaxAmount() + "为最大投资金额", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                else {

                    //投资第二步
                    NetInvestmentStep(minAmount.getText().toString().trim());
                    payDialog.dismiss();
                }

            }


        });

        payDialog.show();

    }

    /**
     * 获取数据
     */
    private void getShowBorrow() {
        HttpParams params = new HttpParams();
        if (null != borroInfo) {
            params.put("borrowEId", borroInfo.getEid());
        }
        if (null != mBaoLiInfo) {
            params.put("borrowEId", mBaoLiInfo.getEid());
        }

        if (null != mInvestmentInfo) {
            params.put("borrowEId", mInvestmentInfo.getEid());
        }

        HttpUtils.post(Config.SHOWBORROW, params, new HttpCallBack(this, "加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if (null != result) {
                    try {
                        res = new BorroDetailsRes(new JSONObject(result.toString()));
//                            mPieChartView.setCurrentValues(80);
//                            mPieChartView.setUnit("总金额$:1000000000000");
//                            mPieChartView.setTitle("11.50%");  percentage
//                            mPieChartView.setConterText("15天     100元起头");

                        if (null != res) {
                            borrowType = res.getBorrowType();    //带给下一个页面
                            //
                            String mPercentage = res.getPercentage().substring(0, res.getPercentage().lastIndexOf("."));
                            mPieChartView.setCurrentValues(Integer.parseInt(mPercentage));
                            mPieChartView.setUnit("总金额￥" + res.getBorrowSum());
                            tvAvailableTenderMoney.setText(res.getAvailableTenderMoney());
                            mPieChartView.setTitle(res.getPercentage() + "%");
                            if (res.getIsDay().equals("1")) {   //表示天
                                mPieChartView.setConterText(res.getBorrowTimeLimit() + "天    " + res.getMinAmount() + "元起投");
                            } else {
                                mPieChartView.setConterText(res.getBorrowTimeLimit() + "个月    " + res.getMinAmount() + "元起投");

                            }

                            //设置投资按钮的状态
                            if (null != res) {
//
                                if (res.getPublished().equals("true")) {
                                    if (res.getBorrowStatus().equals("6")) {
                                        if (Tool.compare_date(res.getAllowTenderTimeStr())) {
                                            mtv_lijiTouzi.setText("立即投资");
                                        } else {
                                            mtv_lijiTouzi.setText("筹募期已结束");
                                        }
                                    } else if (res.getBorrowStatus().equals("8") || res.getBorrowStatus().equals("11") || res.getBorrowStatus().equals("12")) {

                                        mtv_lijiTouzi.setText(getbiaoType(res.getBorrowStatus()));
                                    } else {
                                        //贷加入
                                    }

                                } else if (res.getPublished().equals("false")) {

                                    mtv_lijiTouzi.setText(" 即将发布");
                                }


                            }


                            //设置标类型
                            if (null != res) {
                                String type = res.getBorrowType();
                                if (type.equals("BL")) {
                                    mtvBiaoType.setText("信用贷");
                                } else if (type.equals("CD")) {
                                    mtvBiaoType.setText("抵押贷");
                                } else if(type.equals("DS")){
                                    mtvBiaoType.setText("电商贷");
                                }
                                else {
                                    mtvBiaoType.setText("质押贷");
                                }
                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });

    }


    //写一个公共表示标状体
    public String getbiaoType(String type) {
        if (type.equals("8")) {
            return "满标";
        }
        if (type.equals("11")) {
            return "还款中";
        }
        if (type.equals("12")) {
            return "还款完成";
        }
        return "";

    }


    /**
     * 投资第一步
     */
    private void goInvestment() {
        HttpParams params = new HttpParams();
        if (null != borroInfo) {
            params.put("borrowEId", borroInfo.getEid());
        }
        if (null != mBaoLiInfo) {
            params.put("borrowEId", mBaoLiInfo.getEid());
        }

        HttpUtils.post(Config.TOTENDER, params, new HttpCallBack(this, "加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if (null != result) {
                    try {
                        mInvestmentStepOneRes = new InvestmentStepOneRes(new JSONObject(result.toString()));

                        showDialg();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });


    }

    /**
     * 投资第二步   tenderAmount 投资金额
     */

    private void NetInvestmentStep(final String tenderAmount) {

        HttpParams params = new HttpParams();
        if (null != borroInfo) {
            params.put("borrowEId", borroInfo.getEid());
            params.put("tenderAmount", tenderAmount);

        }
        if (null != mBaoLiInfo) {
            params.put("borrowEId", mBaoLiInfo.getEid());
            params.put("tenderAmount", tenderAmount);
        }
        HttpUtils.post(Config.TENDER, params, new HttpCallBack(this, "加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (null != result) {

                    try {
                        payres = new PayHongBaoRes(new JSONObject(result.toString()));
                        shoPayDialog(tenderAmount);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }


            @Override
            public void onError(String code, String message) {

                Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     *
     *立即支付对话框
     * @param tenderAmount
     */
    private void shoPayDialog(String tenderAmount) {
        View payView = LayoutInflater.from(this).inflate(R.layout.paydialog_layout, null);
        mNextpayDialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        mNextpayDialog.setContentView(payView);
        Window dialogWindow = mNextpayDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置离底部的距离
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 20;
        dialogWindow.setAttributes(lp);

        //增加标密码框
        final LinearLayout ll_biao= (LinearLayout) payView.findViewById(R.id.ll_biao);
        final  ClearEditText et_biao_paypassword=(ClearEditText)payView.findViewById(R.id.et_biao_paypassword);
        if(null!=mInvestmentStepOneRes && mInvestmentStepOneRes.getIsNeedP().equals("1")){
            et_biao_paypassword.setVisibility(View.GONE);
            ll_biao.setVisibility(View.GONE);
        }else {
            et_biao_paypassword.setVisibility(View.VISIBLE);
            ll_biao.setVisibility(View.VISIBLE);
        }

        //
        TextView ishongbao = (TextView) payView.findViewById(R.id.ishongbao);
        TextView netPay = (TextView) payView.findViewById(R.id.netPay);
        final ClearEditText et_paypassword = (ClearEditText) payView.findViewById(R.id.et_paypassword);
        final TextView tv_money = (TextView) payView.findViewById(R.id.tv_money);
        tv_money.setText(tenderAmount);
        GridView hongbaolist = (GridView) payView.findViewById(R.id.hongbaolist);
        hongbaolist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RedList info = (RedList) mhongbaolistAdapter.getItem(position);
                for (int i = 0; i < payres.getRedList().size(); i++) {
                    if (payres.getRedList().get(i).getRedId().equals(info.getRedId())) {
                        payres.getRedList().get(i).setIschick(true);
                        redUserEid = payres.getRedList().get(i).getRedId();
                    } else {
                        payres.getRedList().get(i).setIschick(false);
                    }
                }
                mhongbaolistAdapter.notifyDataSetChanged();
            }
        });
        //判断是否有红包
        if (null != payres && payres.getRedList().size() > 0) {
            hongbaolist.setVisibility(View.VISIBLE);
            if (null != payres) {
                mhongbaolistAdapter = new PayHongbaolistAdapter(TouZiDetailsActivity.this, payres.getRedList());
                hongbaolist.setAdapter(mhongbaolistAdapter);
            }
        } else {
            hongbaolist.setVisibility(View.GONE);
            ishongbao.setVisibility(View.VISIBLE);
        }


        payView.findViewById(R.id.iv_pay_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNextpayDialog.dismiss();
            }
        });

        //忘记密码,首先判断是否市民认证
        payView.findViewById(R.id.tv_forgetPayPasswrod).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //首先判断是否市民认证
                getAccountConfig(1);
            }


        });


        //支付
        netPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(et_paypassword.getText().toString().trim())) {
                    et_paypassword.setShakeAnimation();
                    Toast.makeText(TouZiDetailsActivity.this, "请输入交易密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if(mInvestmentStepOneRes.getIsNeedP().equals("0") && TextUtils.isEmpty(et_biao_paypassword.getText().toString().trim())){
                    et_biao_paypassword.setShakeAnimation();
                    Toast.makeText(TouZiDetailsActivity.this, "请输入标密码", Toast.LENGTH_SHORT).show();
                    return;
                }


                else {

                    // 进行实名认证
                    getAccountConfig(tv_money, et_paypassword,et_biao_paypassword);
                    //


                }
            }


        });


        //一定要show
        mNextpayDialog.show();

    }

    /**
     * @param type
     */
    private void getAccountConfig(final int type) {
        HttpParams params = new HttpParams();
        HttpUtils.post(Config.ACCOUNTCONFIG, params, new HttpCallBack(this, "认证中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if (null != result) {
                    try {
                        CitizenscertificationRes res = new CitizenscertificationRes(new JSONObject(result.toString()));
                        Huifu info = null;
                        if (null != res.getHuifu()) {
                            info = res.getHuifu();
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("capAcntNo", info.getCapAcntNo());
                            params.put("user_id_from", info.getUserIdFrom());
                            params.put("back_notify_url", info.getBackNotifyUrl());
                            params.put("city_id", info.getCityId());
                            params.put("email", info.getEmail());
                            params.put("cust_nm", info.getCustNm());
                            params.put("mchnt_cd", info.getMchntCd());
                            params.put("certif_id", info.getCertifId());
                            params.put("parent_bank_id", info.getParentBankId());
                            params.put("bank_nm", info.getBankNm());
                            params.put("mchnt_txn_ssn", info.getMchntTxnSsn());
                            params.put("mobile_no", info.getMobileNo());
                            params.put("signature", URLEncoder.encode(info.getSignature(), "UTF-8"));
                            //增加两个字段
                            params.put("ver", info.getVer());
                            params.put("page_notify_url", info.getPageNotifyUrl());

                            //去web加载页面
                            Intent intent = new Intent(TouZiDetailsActivity.this, WebActivity.class);
                            intent.putExtra("url", info.getGateWayUrl());
                            intent.putExtra("postData", HtmlFormUtil.makePostHTML(info.getGateWayUrl(), params));
                            intent.putExtra("titile_text", "实名认证");
                            try {
                                intent.putExtra("params", HtmlFormUtil.paramMap2bytes(params));
                                startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            //
                            switch (type) {
                                case 1:
                                    startActivity(new Intent(TouZiDetailsActivity.this, JiaoYiPasswordActivity.class));

                                    break;

                                case 2:

                                    final ExitLoaginDialog exitLoaginDialog = new ExitLoaginDialog(TouZiDetailsActivity.this);
                                    exitLoaginDialog.setContent("账号余额不足,请及时充值 ?", "", "去充值", "取消");
                                    exitLoaginDialog.setCancleListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            exitLoaginDialog.dismiss();
                                        }
                                    });

                                    exitLoaginDialog.setOKListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                            Intent intent = new Intent(TouZiDetailsActivity.this, AccountChongZhiActivity.class);
                                            intent.putExtra("availableMoney", mInvestmentStepOneRes.getAvailableMoney());
                                            startActivity(intent);
                                            exitLoaginDialog.dismiss();

                                        }
                                    });

                                    exitLoaginDialog.show();

                                    break;

                                default:
                            }


                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }


                }
            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });


    }

    /**
     * 支付实名认证
     *
     * @param tv_money
     * @param et_paypassword
     * @param
     */
    private void getAccountConfig(final TextView tv_money, final ClearEditText et_paypassword,final  ClearEditText et_biao_paypassword) {

        HttpParams params = new HttpParams();
        HttpUtils.post(Config.ACCOUNTCONFIG, params, new HttpCallBack(this, "认证中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if (null != result) {
                    try {
                        CitizenscertificationRes res = new CitizenscertificationRes(new JSONObject(result.toString()));
                        Huifu info = null;
                        if (null != res.getHuifu()) {   //Huifu--表示没有这个子节点,说明久市民认证过
                            info = res.getHuifu();
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("capAcntNo", info.getCapAcntNo());
                            params.put("user_id_from", info.getUserIdFrom());
                            params.put("back_notify_url", info.getBackNotifyUrl());
                            params.put("city_id", info.getCityId());
                            params.put("email", info.getEmail());
                            params.put("cust_nm", info.getCustNm());
                            params.put("mchnt_cd", info.getMchntCd());
                            params.put("certif_id", info.getCertifId());
                            params.put("parent_bank_id", info.getParentBankId());
                            params.put("bank_nm", info.getBankNm());
                            params.put("mchnt_txn_ssn", info.getMchntTxnSsn());
                            params.put("mobile_no", info.getMobileNo());
                            params.put("signature", URLEncoder.encode(info.getSignature(), "UTF-8"));

                            //增加两个字段
                            params.put("ver", info.getVer());
                            params.put("page_notify_url", info.getPageNotifyUrl());
                            //去web加载页面
                            Intent intent = new Intent(TouZiDetailsActivity.this, WebActivity.class);
                            intent.putExtra("url", info.getGateWayUrl());
                            intent.putExtra("postData", HtmlFormUtil.makePostHTML(info.getGateWayUrl(), params));
                            intent.putExtra("titile_text", "实名认证");
                            try {
                                intent.putExtra("params", HtmlFormUtil.paramMap2bytes(params));
                                startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            //
                            // 否则就直接支付
                            HttpParams param = new HttpParams();
                            if (null != mInvestmentStepOneRes) {
                                param.put("borrowEId", mInvestmentStepOneRes.getBorrowEId());
                                param.put("tenderAmount", tv_money.getText().toString().trim());
                            }
                            try {
                                if (null != payres) {
                                    param.put("payPassword", DesUtil.encrypt(et_paypassword.getText().toString().trim(), payres.getPublickey()));
                                    param.put("redUserEid", redUserEid);
                                }

                                param.put("tenderPassword",et_biao_paypassword.getText().toString().trim());

                                HttpUtils.post(Config.SAVETENDER, param, new HttpCallBack(TouZiDetailsActivity.this, "支付中...") {
                                    @Override
                                    public void onSucceed(JSONObject result, String code, String message) {
                                        Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
                                        redUserEid = "";
                                        if (null != mNextpayDialog && mNextpayDialog.isShowing()) {
                                            mNextpayDialog.dismiss();
                                        }

                                    }

                                    @Override
                                    public void onError(String code, String message) {
                                        if (!code.equals("") && code.equals("200")) {
                                            Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
                                            mNextpayDialog.dismiss();
                                            // 第一步刷新数据
                                            getShowBorrow();
//
                                            //第二步,回退刷新数据
                                            Intent intent = new Intent();
                                            if (!type.equals("")) {

                                                if (type.equals("1000")) {
                                                    setResult(1000, intent);  //刷新首页数据
                                                    return;
                                                }
                                                if (type.equals("2000")) {
                                                    setResult(2000, intent);  //刷新宝理数据
                                                    return;
                                                }
                                                if (type.equals("3000")) {
                                                    setResult(3000, intent);  //刷抵押数据
                                                    return;
                                                }
                                                if(type.equals("4000")){
                                                    setResult(4000, intent);  //刷质押贷数据
                                                    return;
                                                }
                                                if(type.equals("5000")){
                                                    setResult(5000, intent);  //刷质押贷数据
                                                    return;
                                                }
                                            }


                                        } else {
                                            Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
                                            redUserEid = "";


                                        }

                                    }
                                });

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {

                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(TouZiDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * float保留两位小数
     */


    public String savefloat(float floats) {
        DecimalFormat fnum = new DecimalFormat("##0.00");
        return fnum.format(floats);
    }

    /**
     * 将float 类型保留两位数
     *
     * @param o
     * @return
     */
    public String formatNumber(Object o) {
        try {
            return null == o ? "" : String.format("%.2f", o);
        } catch (Exception e) {

            return "0.00";
        }
    }

}
